package com.sk.gzwater.view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sk.gzewop.R;
import com.sk.gzwater.adapter.ListviewAdapterSplit;
import com.sk.gzwater.util.CommonMethods;
import com.sk.gzwater.util.DBHelper;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursorDriver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQuery;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class ShuichangList extends Activity {
	private List<String> list;
	private List<String> split;
	private ListView shuichangListView;
	private ArrayList<Plant> plantInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shuichanglist);
		setData();
		shuichangListView = (ListView)findViewById(R.id.shuichanglist);
		shuichangListView.setAdapter(new ListviewAdapterSplit(ShuichangList.this, list, split));
		shuichangListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {				
				String resultString =  ((TextView)view.findViewById(R.id.shuichang_name)).getText().toString();
				RadioButton action = (RadioButton)view.findViewById(R.id.shuichang_action);
				action.setChecked(true);
				Intent intent = new Intent();
				intent.putExtra("shuichang_name",resultString);
				intent.putExtra("PlantID", getPlantID(resultString));
				ShuichangList.this.setResult(RESULT_OK,intent);
				finish();
			}
		});
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			Intent requestIntent = getIntent();
			Intent postIntent = new Intent();
			String result = requestIntent.getExtras().getString("default_name");
			postIntent.putExtra("shuichang_name", result);
			postIntent.putExtra("PlantID", getPlantID(result));
			ShuichangList.this.setResult(RESULT_OK, postIntent);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	public void setData(){
		list = new ArrayList<String>();
		split = new ArrayList<String>();
		plantInfo = new ArrayList<Plant>();
		Plant plant = null;
		Cursor cr = CommonMethods.searchData(ShuichangList.this, "select * from PlantInfo", null);
		while (!cr.isLast()) {			
			plant = new Plant();
			plant.PlantID = Integer.valueOf(cr.getString(0));
			plant.PlantName = cr.getString(1);
			plant.OrganizationLevel = Integer.valueOf(cr.getString(2));
			plant.PlantType = Integer.valueOf(cr.getString(3));
			plant.FartherPlantID = Integer.valueOf(cr.getString(4));
			plant.OrderID = Integer.valueOf(cr.getString(5));
			plantInfo.add(plant);
			cr.moveToNext();
		}
		for(int i =0;i<plantInfo.size();i++){
			if(plantInfo.get(i).PlantType ==4){
				split.add(plantInfo.get(i).PlantName);
				list.add(plantInfo.get(i).PlantName);
				for(int j =0;j<plantInfo.size();j++){
					if(plantInfo.get(j).FartherPlantID ==plantInfo.get(i).PlantID){
						list.add(plantInfo.get(j).PlantName);
					}
				}
			}
		}
		
		
	}
	
	public int getPlantID(String plantName){
		int plantID = 1;
		Iterator<Plant> iterator = plantInfo.iterator();
		while (iterator.hasNext()) {
			Plant plant = (Plant) iterator.next();
			if(plant.PlantName.equals(plantName)){
				plantID = plant.PlantID;
			}
		}
		return plantID;
	}
	
	public class Plant{
		public int PlantID;
		public  String PlantName;
		public int OrganizationLevel;
		public int PlantType;
		public int FartherPlantID;
		public int OrderID;
	}
}
